package com.lzy.cloud.account.service.impl;

import com.lzy.cloud.account.dto.AccountWalletDto;
import com.lzy.cloud.account.dto.AccountWalletInfoDto;
import com.lzy.cloud.account.entity.AccountWalletInfo;
import com.lzy.cloud.account.mapper.AccountWalletInfoMapperExt;
import com.lzy.cloud.account.mapper.AccountWalletMapperExt;
import com.lzy.cloud.account.service.AccountWalletInfoService;
import com.lzy.cloud.account.utils.CommonsUtil;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountWalletInfoServiceImpl implements AccountWalletInfoService {

    @Autowired
    private AccountWalletInfoMapperExt accountWalletInfoMapperExt;
    @Autowired
    private AccountWalletMapperExt accountWalletMapperExt;

    @Override
    public Boolean update(AccountWalletDto accountWalletDto) {
        AccountWalletInfoDto accountWalletInfoDto = DozerUtils.map(accountWalletMapperExt.findByObject(accountWalletDto),AccountWalletInfoDto.class);
        Ensure.that(null != accountWalletInfoDto).isTrue("00010004");
        Ensure.that(null != accountWalletInfoDto.getId()).isTrue("00010004");
        //充值
        if(accountWalletDto.getCountType() == 1){
            accountWalletInfoDto.setBalance(accountWalletInfoDto.getBalance().add(accountWalletDto.getBalance()));
        }
        //提现
        if(accountWalletDto.getCountType() == 2){
            accountWalletInfoDto.setBalance(accountWalletInfoDto.getBalance().subtract(accountWalletDto.getBalance()));
        }
        int count = accountWalletInfoMapperExt.updateBalance(accountWalletInfoDto);
        //更新失败，自调
        if(CommonsUtil.isEmpty(count)){
            update(accountWalletDto);
        }
        return true;
    }

    @Override
    public int deleteById(Long id) {
        return accountWalletInfoMapperExt.deleteById(id);
    }

    @Override
    public Long add(AccountWalletInfoDto accountWalletInfoDto) {
        AccountWalletInfo accountWalletInfo = DozerUtils.map(accountWalletInfoDto, AccountWalletInfo.class);
        return accountWalletInfoMapperExt.insertSelective(accountWalletInfo) > 0 ? accountWalletInfo.getId() : 0;
    }

    @Override
    public AccountWalletInfoDto selectAvailableAccount(Long walletId) {
        return accountWalletInfoMapperExt.selectAvailableAccount(walletId);
    }

    @Override
    public AccountWalletInfoDto selectBlockAccount(Long walletId) {
        return accountWalletInfoMapperExt.selectBlockAccount(walletId);
    }

    @Override
    public Boolean updateAccountWalletInfo(AccountWalletInfoDto accountWalletInfoDto) {
        return accountWalletInfoMapperExt.updateAccountWalletInfo(accountWalletInfoDto)>0;
    }

    @Override
    public Boolean updateBalance(AccountWalletInfoDto accountWalletInfoDto) {
        return accountWalletInfoMapperExt.updateBalance(accountWalletInfoDto)>0;
    }

}
